package com.proton.carepatchtemp.utils.net;

import com.android.volley.Response;
import com.apkfuns.logutils.LogUtils;
import com.google.gson.reflect.TypeToken;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.daomanager.ProfileManager;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.utils.FileUtils;
import com.proton.carepatchtemp.utils.JSONUtils;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.oss.OSSUtils;
import com.proton.carepatchtemp.utils.net.pair.ParamPair;
import com.proton.carepatchtemp.utils.net.pair.ResultPair;
import com.proton.carepatchtemp.utils.net.request.JGet;
import com.proton.carepatchtemp.utils.net.request.JPost;
import com.sdsmdg.tastytoast.TastyToast;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * Created by Api on 2016/9/27.
 */

public class PrfCenter extends DataCenter {
    public static final int PS_PRF = 50;
    public static final int PS_PAGE = 1;
    public static final int PS_PRT = 50;
    public static final String add = "/tempprofile/add";
    public static final String edit = "/profile/edit";
    public static final String getList = "/tempprofile/getlists";
    public static final String delete = "/profile/delete";
    public static final String get = "/tempprofile/get";

    public static boolean add(Profile mProfile) {
        if (DataCenter.noNet() || mProfile == null) {
            return false;
        }
        mProfile.setAvatar(OSSUtils.getSaveUrl(mProfile.getAvatar()));
        List params = mProfile.toArray();
        VolleyUtil.addRequest(new JPost(getApi(add), params, new Response.Listener<ResultPair>() {
            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                Profile mProfile = null;
                if (response.isSuccess()) {
                    mProfile = JSONUtils.getObj(msg, Profile.class, "yyyy-MM-dd");
                    if (null != mProfile) {
                        // 插入数据库
                        ProfileManager.insertOrReplace(mProfile);
                        //PrfUtils.savePrf(mProfile);
                        LogUtils.d("form server");
//                        LogUtils.e(mProfile);
                    }
                    LogUtils.d(mProfile);
//                    getlist();
                }
//                updataCache();
                EventBus.getDefault().post(new MainEvent(Codes.NET_PRF_ADD, ret, msg, mProfile));
            }
        }));
        return false;
    }


    public static boolean edit(Profile mDoc) {
        final int code = Codes.NET_PRF_EDIT;
        if (DataCenter.noNet() || mDoc == null) {
            return new MainEvent(code, RET_F, R.string.no_net).post();
        }
        mDoc.setAvatar(OSSUtils.getSaveUrl(mDoc.getAvatar()));
        List params = mDoc.toArray();
        VolleyUtil.addRequest(new JPost(getApi(edit), params, new Response.Listener<ResultPair>() {
            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                Profile prf = null;
                if (response.isSuccess()) {
                    //清除pdf文件重新生成
                    FileUtils.deleteFolderFile(FileUtils.getPdf(), true);
                    prf = JSONUtils.getObj(msg, Profile.class, "yyyy-MM-dd");
                    if (prf != null) {
                        //插入数据库
                        ProfileManager.update(prf);
                        //PrfUtils.savePrf(mProfile);
                        LogUtils.e(prf);
                    }
                }
//                updataCache();
                EventBus.getDefault().post(new MainEvent(code, ret, msg, prf));
            }
        }));
        return false;
    }


    public static boolean getlist() {
        if (DataCenter.noNet()) {
            return false;
        }
        List params = new ArrayList();
        params.add(new ParamPair(Extras.PG, PS_PAGE));
        params.add(new ParamPair(Extras.PS, PS_PRF));
        VolleyUtil.addRequest(new JGet(getApi(getList), params, new Response.Listener<ResultPair>() {
            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                if (response.isSuccess()) {
                    LogUtils.json(msg);
                    Type listType = new TypeToken<ArrayList<Profile>>() {
                    }.getType();
                    List<Profile> profiles = JSONUtils.getObj(msg, listType, "yyyy-MM-dd");
                    //LogUtils.e(profiles);
                    long min = Long.MIN_VALUE;
                    for (Profile prf : profiles) {
                        //LogUtils.e(prf);
                        if (prf.getId() > min) {  //去档案id最大的进行操作
                            min = prf.getId();
                        }
                    }
                    //插入数据库
                    ProfileManager.insertOrReplaceInTx(profiles);
                    EventBus.getDefault().post(new MainEvent(Codes.NET_PRF_GETLIST, ret, ""));
                } else {
                    EventBus.getDefault().post(new MainEvent(Codes.NET_PRF_GETLIST, ret, ""));
                    TastyUtils.makeTextShort(VCare.get().getString(R.string.get_prf_failed), TastyToast.ERROR);
                }
            }
        }));
        return false;
    }

    /**
     * @param id
     * @return
     */
    public static boolean delete(String id) {
        if (DataCenter.noNet()) {
            return false;
        }
        List params = new ArrayList();
        params.add(new ParamPair(Extras.PRF_PROFILEID, id));
        VolleyUtil.addRequest(new JPost(getApi(delete), params, new Response.Listener<ResultPair>() {
            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                if (response.isSuccess()) {
                    ProfileManager.deleteProfile(Long.valueOf(id));
//                    updataCache();
                }
                EventBus.getDefault().post(new MainEvent(Codes.PRF_DELETE, ret, msg));
            }
        }));
        return false;
    }

    public static boolean get(String id) {
        if (DataCenter.noNet()) {
            return false;
        }
        List params = new ArrayList();
        params.add(new ParamPair(Extras.PRF_PROFILEID, id));
        VolleyUtil.addRequest(new JGet(getApi(get), params, new Response.Listener<ResultPair>() {

            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                Profile prf = null;
                if (response.isSuccess()) {
                    prf = JSONUtils.getObj(msg, Profile.class, "yyyy-MM-dd");
                    if (prf != null) {
                        //数据库更新
                        ProfileManager.insertOrReplace(prf);
//                        VCare.getACacheInstance().put(Extras.ACHCHE_PROFILE, prf);
//                        EventBus.getDefault().post(new MainEvent(Codes.NET_ONE_PRF_DATAIL, ret,msg, prf));
                    }
                }

            }
        }));
        return false;
    }

    //档案编码转换表
//map.put('A', "0");
//map.put('C', "1");
//map.put('E', "2");
//map.put('G', "3");
//map.put('H', "4");
//map.put('K', "5");
//map.put('M', "6");
//map.put('P', "7");
//map.put('S', "8");
//map.put('T', "9");
//map.put('Z', "+");

    public static String codeProfileID(String string) {
        return string.replaceAll("0", "A").replaceAll("1", "C").replaceAll("2", "E").replaceAll("3", "G").replaceAll("4", "H").replaceAll("5", "K").replaceAll("6", "M").
                replaceAll("7", "P").replaceAll("8", "S").replaceAll("9", "T").replaceAll("\\+", "Z");
    }

    public static String obtainProfileID(Profile mProfile) {
        Random random = new Random();
        return codeProfileID("" + random.nextInt(10) + random.nextInt(10) + random.nextInt(10) + VCare.get().getUid() + "+" + mProfile.getId() + random.nextInt(10) + random.nextInt(10) + random.nextInt(10));
    }

//    public static boolean updataCache() {
//        if (DataCenter.noNet()) {
//            return false;
//        }
//        List params = new ArrayList();
//        params.add(new ParamPair(Extras.PG, PS_PAGE));
//        params.add(new ParamPair(Extras.PS, PS_PRF));
//        VolleyUtil.addRequest(new JGet(getApi(getList), params, new Response.Listener<ResultPair>() {
//            @Override
//            public void onResponse(ResultPair response) {
//                String ret = response.getName();
//                String msg = response.getValue();
//                LogUtils.e(response);
//                if (response.isSuccess()) {
//                    VCare.getACacheInstance().put(Extras.ACHCHE_PROFILE, msg);
//                } else {
//                    TastyUtils.makeTextShort(VCare.get().getString(R.string.get_prf_failed), TastyToast.ERROR);
//                }
//            }
//        }));
//        return false;
//    }

}
